Micron Document
Livres et Wikis | Archives | Info


Java syntax
part 17/23 Β· 86.0 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
β€’ C explicitly contains a declaration of an abstract method.
β€’ Any of C's superclasses has an abstract method and C neither declares
nor inherits a method that implements it.
β€’ A direct superinterface of C declares or inherits a method (which is
therefore necessarily abstract) and C neither declares nor inherits a
method that implements it.
β€’ A subclass of an abstract class that is not itself abstract may be
instantiated, resulting in the execution of a constructor for the
abstract class and, therefore, the execution of the field initializers
for instance variables of that class.

package org.dwwwp.test;
/**
* @author jcrypto
*/
public class AbstractClass {
private static final String hello;
static {
System.out.println(AbstractClass.class.getName() + ": static block
runtime");
hello = "hello from " + AbstractClass.class.getName();
}
{
System.out.println(AbstractClass.class.getName() + ": instance block
runtime");
}
public AbstractClass() {
System.out.println(AbstractClass.class.getName() + ": constructor
runtime");
}
public static void hello() {
System.out.println(hello);
}
}

package org.dwwwp.test;
/**
* @author jcrypto
*/
public class CustomClass extends AbstractClass {
static {
System.out.println(CustomClass.class.getName() + ": static block
runtime");
}
{
System.out.println(CustomClass.class.getName() + ": instance block
runtime");
}
public CustomClass() {
System.out.println(CustomClass.class.getName() + ": constructor
runtime");
}
public static void main(String[] args) {
CustomClass nc = new CustomClass();
hello();
//AbstractClass.hello();//also valid
}
}

Output:

org.dwwwp.test.AbstractClass: static block runtime
org.dwwwp.test.CustomClass: static block runtime
org.dwwwp.test.AbstractClass: instance block runtime
org.dwwwp.test.AbstractClass: constructor runtime
org.dwwwp.test.CustomClass: instance block runtime
org.dwwwp.test.CustomClass: constructor runtime
hello from org.dwwwp.test.AbstractClass

Enumerations

This language feature was introduced in J2SE 5.0. Technically
enumerations are a kind of class containing enum constants in its body.
Each enum constant defines an instance of the enum type. Enumeration
classes cannot be instantiated anywhere except in the enumeration class
itself.

enum Season {
WINTER, SPRING, SUMMER, AUTUMN
}

Enum constants are allowed to have constructors, which are called when
the class is loaded:

public enum Season {
WINTER("Cold"), SPRING("Warmer"), SUMMER("Hot"), AUTUMN("Cooler");
Season(String description) {
this.description = description;
}
private final String description;
public String getDescription() {
return description;
}
}

Enumerations can have class bodies, in which case they are treated like
anonymous classes extending the enum class:

public enum Season {

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────